/*
  学习目标：通过一个subscribe监听仓库的数据变化
  步骤：
     1. 
     2. 
     3. 
*/

import { legacy_createStore as createStore } from 'redux';
const store = createStore(function (state = { count: 0 }, action) {
  console.warn('action  ----->  ', action);
  if (action.type === 'add') {
    return {
      count: state.count + 1,
    };
  }

  if (action.type === 'des') {
    return {
      count: state.count - 1,
    };
  }

  return state;
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });
// 💥 一定是开启监听后的dispatch才能监听
// 1. 监听: const 解除监听的函数 =  store.subscribe(回调函数)
const unFn = store.subscribe(() => {
  console.log('store.getState().count  ----->  ', store.getState().count);
});
store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });
// 2. 解除监听: 调用解除监听的函数
unFn();

// 💥 解除监听, dispatch不再触发监听
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
